package icu.makka.yygh.order.api;


import icu.makka.yygh.enums.PaymentTypeEnum;
import icu.makka.yygh.model.order.OrderInfo;
import icu.makka.yygh.order.service.OrderInfoService;
import icu.makka.yygh.order.service.PaymentService;
import icu.makka.yygh.order.service.WeChatService;
import icu.makka.yygh.order.service.impl.OrderInfoServiceImpl;
import icu.makka.yygh.order.service.impl.PaymentServiceImpl;
import icu.makka.yygh.result.Result;
import icu.makka.yygh.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/order/weixin")
public class WeChatApiController {

    @Autowired
    private PaymentServiceImpl paymentService;

    @Autowired
    private OrderInfoServiceImpl orderInfoService;

    /**
     * 微信支付成功
     * @param orderId
     * @return
     */
    @GetMapping("/pay/{orderId}")
    public Result pay(@PathVariable("orderId") Long orderId) {
        log.info("微信支付，orderId={}", orderId);
        //获取订单信息
        OrderInfo orderInfo = orderInfoService.getById(orderId);
        //保存支付记录
        paymentService.savePaymentInfo(orderInfo, PaymentTypeEnum.WEIXIN.getStatus());
        //获取订单交易号
        String outTradeNo = orderInfo.getOutTradeNo();
        log.info("获取订单交易号，orderInfo={}", orderInfo);
        //调用业务层
        paymentService.paySuccess(outTradeNo, PaymentTypeEnum.WEIXIN.getStatus(), null);

        return Result.ok();
    }
}
